/**
 * @file 预约时间
 * @author yangjiangming <449654337@qq.com>
 * @since 2018/12/14 16:38:57
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laytpl'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laytpl = layui.laytpl;
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [] ,
        page: true,
        limit: json.pageSize,
        limits: [50,100,150],
        height: 'full-140'
    });
    // laypage.render(zmnPage.laypageOptions(json));

    $("#reset").on("click", function () {
        // $('#servCategId').val('');
        // $('#categOneId').val('');
        // $('#categTwoId').val('');
        $("#servCategId option").attr("selected", false);
        $("#categOneId option").attr("selected", false);
        $("#categTwoId option").attr("selected", false);
        form.render();
    });

    // 服务分类
    form.on('select(servCategId)', function(obj){
        triggerOptions(1, obj.value);
    });
    // 一级分类
    form.on('select(categOneId)', function(obj){
        triggerOptions(2, obj.value);
    });
    function triggerOptions(level, parentId){
        let options = (level == 1 ? json.categOneList : json.categTwoList).filter(p => p.parentId == parentId).map(p => {
            return p.id == (level == 1 ? json.query.categOneId : json.query.categTwoId) ? '<option value="'+p.id+'" selected="selected">'+p.name+'</option>' : '<option value="'+p.id+'">'+p.name+'</option>';
        });
        options.unshift('<option value=""></option>');
        (level == 1 ? $('#categOneId') : $("#categTwoId")).html(options);
        form.render("select");
    }
    if(json.query.servCategId){
        triggerOptions(1, json.query.servCategId);
    }
    if(json.query.categOneId){
        triggerOptions(2, json.query.categOneId);
    }

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map(item => {
            return {"servCategId": item.servCategId, "categOneId": item.categOneId, "categTwoId": item.categTwoId}
        });
    });

    table.on("toolbar(zmn-table)", function (obj) {

        if(obj.event == "batchClose") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一条数据！");
            }
            batchCloseDays(json.query.channelId, ids);
        }
        if(obj.event == "batchOpen") {
            batchOpenDays(json.query.channelId);
        }
    });

    exports("channelconfig_list_js");
});

function closeDays(channelId, servCategId, categOneId, categTwoId){
    zmnLayUtil.openIframeCustom(layer, "/channel/config/close.action?channelId="+channelId+"&servCategId="+servCategId+"&categOneId="+categOneId+"&categTwoId="+categTwoId, {title: "关闭库存", shade: 0.9, area:["80%", "60%"]});
}

function openDays(channelId, servCategId, categOneId, categTwoId){
    zmnLayUtil.openIframeCustom(layer, "/channel/config/open.action?channelId="+channelId+"&servCategId="+servCategId+"&categOneId="+categOneId+"&categTwoId="+categTwoId, {title: "开启库存", shade: 0.9, area:["80%", "60%"]});
}

function batchCloseDays(channelId, categIds){
    var categoryListJsonStr = JSON.stringify(categIds);
    let storeageCategKey = "orderstock_checked_categ_channel_"+channelId;
    sessionStorage.setItem(storeageCategKey, categoryListJsonStr);
    console.log("storeageCategKey:" + storeageCategKey);
    zmnLayUtil.openIframeCustom(layer, "/channel/config/batchClose.action?channelId="+channelId+"&storeageCategKey="+storeageCategKey, {title: "关闭库存", shade: 0.9, area:["80%", "60%"]});
}

function batchOpenDays(channelId){
    zmnLayUtil.openIframeCustom(layer, "/channel/config/batchOpen.action?channelId="+channelId, {title: "关闭库存", shade: 0.9, area:["60%", "80%"]});
}